#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json/json.h>
#include <json/json_util.h>

#include "read_json_db.h"
#include "base64.h"
#include "sha512.h"

int main( int argc, char **argv )
{
	int iret;
	size_t dlen;
	uint8_t buf[128],
	seed[64]={ 
		'1','2','3','4','5','6','7','8',
		'9','0','1','2','3','4','5','6',
		'7','8','9','0','1','2','3','4',
		'5','6','7','8','9','0','1','2',
		'1','2','3','4','5','6','7','8',
		'9','0','1','2','3','4','5','6',
		'7','8','9','0','1','2','3','4',
		'5','6','7','8','9','0','1','2'},
	seed64[90],
	password[64],sha[64],password64[90];

	memset( password, 0, 64 );
	password[0]='a';
	password[1]='s';
	password[2]='d';

	printf("Seed:\n");
	print_hex( seed, 64, 16 );
	dlen = 90;
	iret = base64_encode( seed64, &dlen, seed, 64 );
	switch(iret)
	{
		case POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL:
			printf("POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL\n");
			break;
		case POLARSSL_ERR_BASE64_INVALID_CHARACTER:
			printf("POLARSSL_ERR_BASE64_INVALID_CHARACTER\n");
			break;
	}
	printf("Seed base64:\n");
	printf("%s\n",seed64);

	printf("Password:\n");
	print_hex( password, 64, 16 );

	memcpy( buf, password, 64 );
	memcpy( &buf[64], seed, 64 );
	printf("Buf:\n");
	print_hex( buf, 128, 16 );

	sha512( buf, 128, sha, 0 );
	printf("Password hash:\n");
	print_hex( sha, 64, 16 );

	dlen=89;
	iret = base64_encode( password64, &dlen, sha, 64 );
	switch(iret)
	{
		case POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL:
			printf("POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL\n");
			break;
		case POLARSSL_ERR_BASE64_INVALID_CHARACTER:
			printf("POLARSSL_ERR_BASE64_INVALID_CHARACTER\n");
			break;
	}
	//password64[88]=0x0;
	printf("Password base64:\n");
	printf( "%s\n", password64 );

	return 0;
}
